﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.KhoanTienNhapTruongService;
using eTMS.Controllers.KhoanTienService;

namespace eTMS.Controllers
{
    public class KhoanTienNhapTruongController : KhoanTienNhapTruongControllerBase
    {
        public KhoanTienNhapTruongController()
            : base()
        {
        }

        private DataTable _tblRemoved = null;
        public void InitTable()
        {
            DataTable table = Table;
            table.Columns.Add("TENKHOANTIEN", typeof(string));
            KhoanTienController khoanTienController = new KhoanTienController();
            foreach (DataRow row in table.Rows)
            {
                khoanTienController.LoadObject((int)row["KHOANTIEN_ID"]);
                row["TENKHOANTIEN"] = khoanTienController.KhoanTienObject.TenKhoanTien;
            }
            _tblRemoved = table.Clone();
            Commons.Common.AddCheckedToTable(table);
        }
        public void AddKhoanTien(KhoanTienEntity khoanTienEntity)
        {
            if (!CheckExistsKhoanTien(khoanTienEntity.KhoanTien_ID))
            {
                DataRow[] rows = _tblRemoved.Select("KHOANTIEN_ID=" + khoanTienEntity.KhoanTien_ID.ToString());
                if(rows.Length>0)
                {
                    foreach (DataRow row in rows)
                    {
                        Table.ImportRow(row);
                        _tblRemoved.Rows.Remove(row);
                    }
                }
                else
                {
                    DataRow row = Table.NewRow();
                    row["CHECKED"] = false;
                    row["KHOANTIEN_ID"] = khoanTienEntity.KhoanTien_ID;
                    row["TENKHOANTIEN"] = khoanTienEntity.TenKhoanTien;
                    row["SoTien"] = 0;
                    row["KhoanTienNhapTruong_ID"] = 0;
                    Table.Rows.Add(row);
                }
            }
        }
        public void RemoveKhoanTien()
        {
            List<DataRow>ls=new List<DataRow>();
            foreach (DataRow row in Table.Rows)
            {
                if((bool)row["CHECKED"])
                {
                    ls.Add(row);
                }
            }
            foreach (DataRow row in ls)
            {
                _tblRemoved.ImportRow(row);
                Table.Rows.Remove(row);
            }
        }
        private bool CheckExistsKhoanTien(int KhoanTien_ID)
        {
            return Table.Select("KHOANTIEN_ID=" + KhoanTien_ID.ToString()).Length > 0;
        }
        public void SaveData()
        {
            foreach (DataRow row in Table.Rows)
            {
                KhoanTienNhapTruongObject = MapDataRowToObject(row);
                if(KhoanTienNhapTruongObject.KhoanTienNhapTruong_ID>0)
                {
                    Update();
                }
                else
                {
                    AddNew();
                }
            }
        }
    }
}
